<?php
/** 
 * Plugin 'rdr_template' for the 'ameos_formidable' extension.
 *
 * @author	Jerome Schneider <typo3dev@ameos.com>
 */

class tx_rdrtemplate extends formidable_mainrenderer {
	
	var $aCustomTags	= array();
	var $aExcludeTags	= array();
	var $sTemplateHtml	= FALSE;
	
	function getTemplatePath() {

		$sPath = $this->_navConf("/template/path/");
		if($this->oForm->isRunneable($sPath)) {
			$sPath = $this->callRunneable(
				$sPath
			);
		}

		if(is_string($sPath)) {
			return $this->oForm->toServerPath($sPath);
		}
		
		return "";
	}

	function getTemplateSubpart() {
		 $sSubpart = $this->_navConf("/template/subpart/");
		 if($this->oForm->isRunneable($sSubpart)) {
			$sSubpart = $this->callRunneable(
				$sSubpart
			);
		}

		return $sSubpart;
	}

	function getTemplateHtml() {

		if($this->sTemplateHtml === FALSE) {
			
			$mHtml = "";
			$sPath = $this->getTemplatePath();

			if(!empty($sPath)) {
				if(($sSubpart = $this->getTemplateSubpart()) !== FALSE) {
					$mHtml = t3lib_parsehtml::getSubpart(
						t3lib_div::getUrl($sPath),
						$sSubpart
					);
				} else {
					$mHtml = t3lib_div::getUrl($sPath);
				}

				if(trim($mHtml) == "") {
					$this->oForm->mayday("RENDERER TEMPLATE - The given template (<b>'" . $sPath . "'</b> with subpart marquer <b>'" . $sSubpart . "'</b>) <b>returned an empty string</b> - Check your template");
				}

			} elseif(($mHtml = $this->_navConf("/html")) !== FALSE) {
				
				if(is_array($mHtml)) {
					if($this->oForm->isRunneable($mHtml)) {
						$mHtml = $this->callRunneable($mHtml);
					} else {
						$mHtml = $mHtml["__value"];
					}
				}
			}

			$this->sTemplateHtml = $mHtml;
		}

		return $this->sTemplateHtml;
	}

	function _render($aRendered) {

		$aRendered = $this->beforeDisplay($aRendered);

		$this->oForm->_debug($aRendered, "RENDERER TEMPLATE - rendered elements array");

		if(($sErrorTag = $this->_navConf("/template/errortag/")) === FALSE) {
			if(($sErrorTag = $this->_navConf("/html/errortag")) === FALSE) {
				$sErrorTag = "errors";
			}
		}

		if($this->oForm->isRunneable($sErrorTag)) {
			$sErrorTag = $this->callRunneable(
				$sErrorTag
			);
		}

		/*$aLabels		= $this->oForm->oDataHandler->_getHumanFormData();
		$aLabels		= $aLabels["labels"];

		if(($aTags = $this->_navConf("/template/customtags")) !== FALSE) {
			$this->aCustomTags = $this->oForm->_getCustomTags($aTags);
			reset($this->aCustomTags["values"]);

			while(list($sKey, $sValue) = each($this->aCustomTags["values"])) {
				// on substitue les balises dans les customtags
				$this->aCustomTags["values"][$sKey] = $this->oForm->_parseTemplateCode(
					$sValue,
					$aRendered,
					array_merge($this->aExcludeTags, array($sErrorTag, $sErrorTag . ".cssdisplay")),
					FALSE,
					array_merge(
						$aLabels,
						$this->aCustomTags["labels"]
					)
				);
			}
		}*/

		$aRendered = $this->displayOnlyIfJs($aRendered);
		$mHtml = $this->getTemplateHtml();
		$sForm = $this->oForm->_parseTemplateCode(
			$mHtml,
			array_merge($aRendered, (is_array($this->aCustomTags["values"]) ? $this->aCustomTags["values"] : array())),
			array_merge($this->aExcludeTags, array($sErrorTag, $sErrorTag . ".cssdisplay")),
			FALSE/*,
			array_merge(
				$aLabels,
				(is_array($this->aCustomTags["labels"]) ? $this->aCustomTags["labels"] : array())
			)*/
		);

		$aErrors = array();
		$aCompiledErrors = array();
		$aErrorKeys = array_keys($this->oForm->_aValidationErrors);
		while(list(, $sRdtName) = each($aErrorKeys)) {
			$sShortRdtName = $this->oForm->aORenderlets[$sRdtName]->_getNameWithoutPrefix();
			if(trim($this->oForm->_aValidationErrors[$sRdtName]) !== "") {
				
				/*$aCompiledErrors[] = $this->oForm->_aValidationErrors[$sRdtName];
				$aErrors[$sShortRdtName] = $this->oForm->_aValidationErrors[$sRdtName];
				$aErrors[$sShortRdtName . "."]["tag"] = "<span class='errors'>" . $this->oForm->_aValidationErrors[$sRdtName] . "</span>";*/

				$aErrors[$sShortRdtName] = $this->oForm->_aValidationErrors[$sRdtName];
				if(($sErrWrap = $this->_navConf("/template/errortagwrap")) !== FALSE) {

					if($this->oForm->isRunneable($sErrWrap)) {
						$sErrWrap = $this->callRunneable($sErrWrap);
					}
					
					$sErrWrap = $this->oForm->_substLLLInHtml($sErrWrap);
				} else {
					$sErrWrap = "<span class='errors'>|</span>";
				}

				$aErrors[$sShortRdtName . "."]["tag"] = str_replace(
					"|",
					$this->oForm->_aValidationErrors[$sRdtName],
					$sErrWrap
				);

				if($this->isTrue("/template/errortagcompilednowrap")) {
					$aCompiledErrors[] = $this->oForm->_aValidationErrors[$sRdtName];
				} else {
					$aCompiledErrors[] = str_replace("|", $this->oForm->_aValidationErrors[$sRdtName], $sErrWrap);
				}
			}
		}

		if(strtolower(trim($this->_navConf("/template/errortagcompilednobr"))) == "true") {
			$aErrors["__compiled"] = implode("", $aCompiledErrors);
		} else {
			$aErrors["__compiled"] = implode("<br />", $aCompiledErrors);
		}

		$aErrors["cssdisplay"] = ($this->oForm->oDataHandler->_allIsValid()) ? "none" : "block";
		//$aHelp = array_keys($aErrors);
		//sort($aHelp);
		//$aErrors["help"] = t3lib_div::view_array($aHelp);

		$sGlobal = $this->oForm->_parseTemplateCode(
			$sForm,
			array(
				$sErrorTag => $aErrors,
			),
			$this->aExcludeTags,
			$this->_defaultTrue("/template/clearmarkersnotused")
		);

		return $this->_wrapIntoForm($sGlobal);
	}

	function beforeDisplay($aRendered) {

		if(($aUserObj = $this->_navConf("/beforedisplay/")) !== FALSE) {

			if($this->oForm->isRunneable($aUserObj)) {
				$aRendered = $this->callRunneable(
					$aUserObj,
					$aRendered
				);
			}
		}

		if(!is_array($aRendered)) {
			$aRendered = array();
		}

		reset($aRendered);
		return $aRendered;
	}

	function cleanBeforeSession() {
		$this->sTemplateHtml = FALSE;
		$this->baseCleanBeforeSession();
	}
}


	if (defined("TYPO3_MODE") && $TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/rdr_template/api/class.tx_rdrtemplate.php"])	{
		include_once($TYPO3_CONF_VARS[TYPO3_MODE]["XCLASS"]["ext/ameos_formidable/api/base/rdr_template/api/class.tx_rdrtemplate.php"]);
	}

?>